##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

import types


class Event:
    '''
    Event perform the connected actions in the thread that emit it, and block the execution.
    '''

    def __init__(self):
        self.actions = []

    def disconnect(self, action):
        if(isinstance(action, types.FunctionType) or isinstance(action, types.MethodType)):
            if(action in self.actions):
                self.actions.remove(action)

    def connect(self, action):
        if(isinstance(action, types.FunctionType) or isinstance(action, types.MethodType)):
            self.actions.append(action)

    def emit(self, *argv, **kargv):
        for act in self.actions:
            act(*argv, **kargv)
